﻿using SqlSugar;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 

        private void button1_Click(object sender, EventArgs e)
        {
            // 指定生成模型的目录
            string outputDirectory = this.textBox1.Text.Trim();
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=3pb3262fk244.vicp.fun:11318;Database=AccountBook;Uid=sa;Pwd=123456;", // 数据库连接字符串
                DbType = SqlSugar.DbType.SqlServer, // 数据库类型
                IsAutoCloseConnection = true, // 自动关闭数据库连接
                InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
            });
            // 获取所有表名
            var allTables = db.DbMaintenance.GetTableInfoList(true);

            // 遍历每个表，生成模型类
            foreach (var table in allTables)
            {
                // 获取表的列信息
                var columns = db.DbMaintenance.GetColumnInfosByTableName(table.Name);

                // 生成模型类代码
                string className = table.Name; // 将表名转换为驼峰命名法作为类名
                string classCode = GenerateClassCode(className, columns);

                // 将代码写入文件
                string filePath = Path.Combine(outputDirectory, $"{className}.cs");
                File.WriteAllText(filePath, classCode);
            }

            MessageBox.Show("模型生成完成！");

        }
        private string GenerateClassCode(string className, List<DbColumnInfo> columns)
        {
            using (var writer = new StringWriter())
            {
                writer.WriteLine("using System;");
                writer.WriteLine();
                writer.WriteLine($"namespace YourNamespace // 替换为你的命名空间");
                writer.WriteLine("{");
                writer.WriteLine($"    public class {className}");
                writer.WriteLine("    {");

                foreach (var column in columns)
                {
                    string propertyName = column.DbColumnName; // 将列名转换为驼峰命名法作为属性名
                    string propertyType = MapColumnTypeToCSharpType(column.DataType);

                    // 生成属性代码
                    writer.WriteLine($"        public {propertyType} {propertyName} {{ get; set; }}");
                }

                writer.WriteLine("    }");
                writer.WriteLine("}");

                return writer.ToString();
            }
        }

        private string MapColumnTypeToCSharpType(string dbType)
        {
            // 这里是一个简单的映射，你可能需要根据你的数据库类型添加更多的映射
            switch (dbType.ToLower())
            {
                case "int":
                    return "int";
                case "varchar":
                case "nvarchar":
                case "text":
                    return "string";
                case "datetime":
                    return "DateTime";
                case "decimal":
                    return "decimal";
                // 添加更多的映射...
                default:
                    return "object";
            }
        }

        //private string ToCamelCase(this string str)
        //{
        //    if (string.IsNullOrEmpty(str) || char.IsLower(str, 0))
        //        return str;

        //    return char.ToLower(str[0], System.Globalization.CultureInfo.InvariantCulture) + str.Substring(1);
        //}


    }
}
